package com.m.demo.proxy;

import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
        //1. 静态代理
//        HouseSubject houseSubject = new RealHouseSubject();
//        HouseProxy proxy = new HouseProxy(houseSubject);
//        proxy.rentHouse();
//        proxy.saleHouse();
        //2.JDK动态代理
        //代理接口
//        HouseSubject houseSubject =  new RealHouseSubject();
//        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(houseSubject.getClass().getClassLoader(),
//                                                    new Class[]{HouseSubject.class},
//                                                    new JDKInvocation(houseSubject));
//        proxy.rentHouse();
//        proxy.saleHouse();
//        //代理类——jDK不支持
//        HouseSubjectClass subjectClass = new HouseSubjectClass();
//        HouseSubjectClass proxy2 = (HouseSubjectClass) Proxy.newProxyInstance(subjectClass.getClass().getClassLoader(),
//                                                                                new Class[]{HouseSubjectClass.class},
//                                                                                new JDKInvocation(subjectClass));
//        proxy2.rentHouse();
        //CGLib动态代理
        //1.代理接口
        HouseSubject houseSubject = new RealHouseSubject();
        HouseSubject proxy = (HouseSubject) Enhancer.create(houseSubject.getClass(), new CGLibIntercepter(houseSubject));
        proxy.rentHouse();
        //2.代理类
        HouseSubjectClass subjectClass = new HouseSubjectClass();
        HouseSubjectClass proxy2 = (HouseSubjectClass) Enhancer.create(subjectClass.getClass(), new CGLibIntercepter(subjectClass));
        proxy.rentHouse();
    }
}
